package com.dexilaonian.activity;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.dexilaonian.MainActivity;
import com.dexilaonian.R;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.PreferencesUtil;
import com.mob.tools.gui.ViewPagerAdapter;

import java.util.ArrayList;

public class GuideActivity extends AppCompatActivity {

    private ArrayList<View> mViewList;
    private ViewPager guideViewpager;
    private ArrayList<ImageView> mDots;
    private LinearLayout viewpager_liner;
    private ImageView experience;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        guideViewpager = (ViewPager) findViewById(R.id.guide_viewpager);
        viewpager_liner = (LinearLayout) findViewById(R.id.viewpager_linerlayout2);
        mViewList = new ArrayList<View>();
        LayoutInflater lf = getLayoutInflater().from(GuideActivity.this);
        View view1 = lf.inflate(R.layout.we_indicator1, null);
        View view2 = lf.inflate(R.layout.we_indicator2, null);
        View view3 = lf.inflate(R.layout.we_indicator3, null);
        experience = (ImageView) view3.findViewById(R.id.experience);
        mViewList.add(view1);
        mViewList.add(view2);
        mViewList.add(view3);
        //initDots();
        init();
        ViewPagerAda adaper = new ViewPagerAda(mViewList);
        guideViewpager.setAdapter(adaper);
    }
    private void initDots() {
        mDots = new ArrayList<ImageView>();//底部圆点集合的初始化
        for (int i = 0; i < 3; i++) {//根据界面数量动态添加圆点
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(45, 45));//设置ImageView的宽度和高度
            imageView.setPadding(10, 10, 10, 10);//设置圆点的Padding，与周围的距离
            imageView.setImageResource(R.drawable.huiyuandian);//设置图片
            mDots.add(imageView);//将该图片添加到圆点集合中
            viewpager_liner.addView(imageView);//将图片添加到LinearLayout中
        }
        mDots.get(0).setImageResource(R.drawable.heiyuandian);
    }

    private void init() {
        guideViewpager.setCurrentItem(0);
        guideViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.e("TTTPOSI", position+"");
                if (position == 2) {
                    experience.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(GuideActivity.this, SplashActivity.class));
                            PreferencesUtil.putString(GuideActivity.this, Constant.SPLASH1, "SPLASH");
                            finish();
                        }
                    });
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    public class ViewPagerAda extends PagerAdapter {

        private  ArrayList<View> mViewList;

        public ViewPagerAda(ArrayList<View> mViewList) {
            this.mViewList = mViewList;
        }

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));
            return mViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));
        }
    }
}
